<?php

class AdminPostsController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		$posts = Post::where('status','=','publish')->paginate(10);
		return View::make('admin.posts/index',compact('posts'));
	}


	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		return View::make('admin.posts.create');
	}


	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		$rules = array('title' => 'required|min:3');
		$messages = array(
		    'title.required' => '标题是必填的.',
		    'min:3'	=>	'标题文字应该大于3个'
		);
		$validator = Validator::make(Input::all(), $rules,$messages);
		if ($validator->fails()){
			return Redirect::route('admin.posts.create')
			->withErrors($validator)
			->withInput();
		}

		$result = Post::insert(array(
			'title' 	=> Input::get('title'),
			'content'	=> Input::get('content'),
			'status'	=> 'publish',
			'slug'		=> Input::get('title'),
			'type'		=> 'post',
			'created_at'=> date('Y-m-d H:i:s'),
			'updated_at'=> date('Y-m-d H:i:s'),
			));
		return Redirect::route('admin.posts.index', compact('result'));
	}


	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}


	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		$post = Post::find($id);
		return View::make('admin.posts.edit',compact('post'));
	}


	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		$rules = array('title' => 'required|min:3');
		$messages = array(
		    'title.required' => '标题是必填的.',
		    'min:3'	=>	'标题文字应该大于3个'
		);
		$validator = Validator::make(Input::all(), $rules,$messages);
		if ($validator->fails()){
			return Redirect::route('admin.posts.create')
			->withErrors($validator)
			->withInput();
		}

		$post = Post::find($id);
		$post ->title = Input::get('title');
		$post ->content = Input::get('content');
		$post ->updated_at = date('Y-m-d H:i:s');
		$post -> save();

		return Redirect::route('admin.posts.index');
	}


	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		Post::destroy($id);
		return Redirect::route('admin.posts.index');
	}


}
